<template>
  <div class="order">
    <div class="header">填写地址</div>
    <van-address-edit
      :area-list="areaList"
      show-delete
      show-set-default
      show-search-result
      :search-result="searchResult"
      :area-columns-placeholder="['请选择', '请选择', '请选择']"
      @save="onSave"
      @delete="onDelete"
      @change-default="changeDefault"
      @change-detail="onChangeDetail"
    />
    <van-icon @click="ret" name="arrow-left" />
  </div>
</template>

<script>
import { Toast } from "vant";
import areaList from "../assets/area";
import addressModel from "../model/addressModel";
export default {
  data() {
    return {
      areaList,
      searchResult: [],
    };
  },

  methods: {
    onSave(content) {
      // console.log(content);
      addressModel.query().then((res) => {
        res.data.forEach((item) => {
          addressModel.updata(item.id, { isDefault: false });
          console.log(res.data);
        });
      });
      let adress =
        content.province +
        content.city +
        content.county +
        content.addressDetail;
      let info = {
        username: localStorage.getItem("username"),
        name: content.name,
        tel: content.tel,
        province: content.province,
        city: content.city,
        county: content.county,
        addressDetail: content.addressDetail,
        isDefault: content.isDefault,
        adress,
      };
      addressModel.add(info).then((res) => {
        console.log(res.data);
      });

      Toast("保存成功,可以去结算啦！");
    },
    onDelete() {
      Toast("delete");
    },
    onChangeDetail(val) {
      if (val) {
        this.searchResult = [
          {
            name: "大分路",
            address: "希望人人暴富村",
          },
        ];
      } else {
        this.searchResult = [];
      }
    },
    changeDefault() {
      console.log(this.checked);
    },
    ret() {
      this.$router.go(-1);
    },
  },
};
</script>
<style lang="scss" scoped>
$main: #c0aa95;
.header {
  line-height: 40px;
  font-size: 20px;
  text-align: center;
  padding-top: 10px;
  background-color: #eae9e5;
}
.iconfont {
  margin-top: 5px;
}
.van-icon-arrow-left {
  font-size: 20px;
  font-weight: 700;
  color: $main;
  position: fixed;
  left: 15px;
  top: 18px;
  box-sizing: content-box;
}
</style>